package hot_patch_client_service

import (
	"encoding/json"
	"gitee.com/zaper/patch/database"
	"gitee.com/zaper/private-logger"
)

func register(message []byte) {
	// 数据反序列化
	hotPatchClientModel := &HotPatchClientModel{}
	err := json.Unmarshal(message, hotPatchClientModel)
	if err != nil {
		logger.Errorf("Unmarshal json fail, err is %s", err)
		return
	}
	//格式化sql
	dbManager := database.GetDataBaseManager()
	count, err := dbManager.QueryCountSql(database.SqlInfos["hot_patch_client_query_count"],
		hotPatchClientModel.ProcessName)
	// 入库
	if count == 0 {
		err = dbManager.PrepareExecSql(database.SqlInfos["hot_patch_client_insert"], hotPatchClientModel.ProcessName,
			hotPatchClientModel.ProcessIp, hotPatchClientModel.ProcessPort, hotPatchClientModel.ProcessType,
			hotPatchClientModel.DateTime)
	} else {
		err = dbManager.PrepareExecSql(database.SqlInfos["hot_patch_client_update"], hotPatchClientModel.ProcessName,
			hotPatchClientModel.ProcessIp, hotPatchClientModel.ProcessPort, hotPatchClientModel.ProcessType,
			hotPatchClientModel.DateTime, hotPatchClientModel.ProcessName)
	}
	if err != nil {
		logger.Errorf("BatchExecSql fail, err is %s", err)
		return
	}
}
